# -*- coding: utf-8 -*-
'''
Created on 07/03/2012

@author: Caio Alves
'''
from django.db import models
from tagging.fields import TagField
from tagging.models import Tag
from django.core.urlresolvers import reverse
import datetime
from stdimage import *
from django.template.defaultfilters import slugify
from django.db.models import signals

CATEGORIA_CHOICES = (
                     ('Pro','Promoções'),
                     ('Nov','Novidades'),
                     ('Var','Variados'),
                     ('Seg','Segurança'),
                     ('Tec','Tecnologia'),
                     ('Dic','Dicas')
                     )
class Galeria(models.Model):
    titulo = models.CharField('Título',max_length=50)
    imagem = models.ImageField(upload_to='media')
    dt_publicacao = models.DateTimeField('Data de publicação',default=datetime.datetime.now())
    autor = models.CharField(max_length=50)
    comentario = models.CharField('Comentário',max_length=255)
    class Meta:
        verbose_name_plural = 'Galeria de imagens'
    def __unicode__(self):
        return self.titulo

class Noticias(models.Model):
    publicar=models.BooleanField(default=True)
    titulo = models.CharField('Título',max_length=100)
    subtitulo = models.CharField(max_length=200)
    chamada = models.CharField(max_length=15, choices = CATEGORIA_CHOICES)
    autor = models.CharField(max_length=50)
    conteudo = models.TextField('Conteúdo')
    publicacao = models.DateTimeField('Data de publicação',default=datetime.datetime.now(),blank=True)
    imagem_destaque=models.ImageField(upload_to='noticia')
    imagens = models.ForeignKey(Galeria,blank=True,null=True)
    videos = models.CharField('GaleriaVideos',max_length = 70,blank=True,null=True)
    fonte = models.CharField(max_length=100)
    tags = TagField()
    slug = models.SlugField(max_length=100, blank=True)
    
    def set_tags(self, tags):
        Tag.objects.update_tags(self, tags)

    def get_tags(self):
        return Tag.objects.get_for_object(self)

    def get_absolute_url(self):
        return reverse('classificados.views.noticia', kwargs={'slug': self.slug})

    class Meta:
        verbose_name_plural = 'Cadastro de notícias'
    def __unicode__(self):
        return self.titulo
def noticias_pre_save(signal, instance, sender, **kwargs):
    instance.slug = slugify(instance.titulo)

signals.pre_save.connect(noticias_pre_save, sender=Noticias)